package com.crm.system.service.impl.cluePoolManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.cluePoolManager.CluePoolPrivate;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.qo.cluePoolManager.CluePoolPrivateQO;
import com.crm.model.vo.cluePoolManager.CluePoolPrivateVO;
import com.crm.service.cluePoolManager.CluePoolPrivateDbService;
import com.crm.system.service.cluePoolManager.CluePoolPrivateService;
import com.crm.system.service.permissionManagement.SysCompanyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @author wll
 * @date 2020/12/28 11:16
 */
@Service
public class CluePoolPrivateServiceImpl implements CluePoolPrivateService {

    @Autowired
    private SysCompanyService sysCompanyService;

    @Autowired
    private CluePoolPrivateDbService cluePoolPrivateDbService;


    /**
     * @author wll
     * @date 2020/12/29 10:00
     * info 用户模块-我的线索池
     * @param pageIndex
     * @param pageSize
     * @param  cluePoolPrivateInfo  搜索条件
     * @param sort 排序条件
     * @param sortOrder 正序/倒序
     * @return
     */
    @Override
    public DhPageInfo<CluePoolPrivateVO> getList(Integer pageIndex, Integer pageSize, CluePoolPrivateQO cluePoolPrivateInfo, String sort, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<CluePoolPrivate> cluePoolPrivates = cluePoolPrivateDbService.selectCluePoolPrivateBySearchInfo(cluePoolPrivateInfo,sort,sortOrder);
        PageInfo<CluePoolPrivate> pageInfo = new PageInfo<>(cluePoolPrivates);
        List<CluePoolPrivateVO> result = new ArrayList<CluePoolPrivateVO>();
        for (CluePoolPrivate dbItem : pageInfo.getList()) {
            result.add(new CluePoolPrivateVO(dbItem));
        }
        return new DhPageInfo<CluePoolPrivateVO>(pageInfo, result);
    }

    @Override
    public CluePoolPrivate selectPoolPrivateById(String poolPrivateId) {
        CluePoolPrivate cluePoolPrivate = cluePoolPrivateDbService.selectCluePoolPrivateById(poolPrivateId);
        return cluePoolPrivate;
    }

    /**
     * @author wll
     * @date 2021/02/01 11:30
     * info 用户模块-我的线索池-修改
     * @param cluePoolPrivateQO
     * @return
     */
    @Override
    public Boolean update(CluePoolPrivateQO cluePoolPrivateQO) {
        return cluePoolPrivateDbService.update(cluePoolPrivateQO);
    }

    /**
     * @author wll
     * @date 2021/02/01
     * info 用户模块-我的线索池-删除
     * @param id 线索池id
     * @return
     */
    @Override
    public Boolean delete(String id) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        return cluePoolPrivateDbService.delete(id,userAccount.getAccountId(),new Date());
    }


    /**
     * @author wll
     * info 用户模块-创建线索池
     */
    @Override
    public Boolean add(CluePoolPrivateQO cluePoolPrivateQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CluePoolPrivate cluePoolPrivate = new CluePoolPrivate();
        BeanUtils.copyProperties(cluePoolPrivateQO, cluePoolPrivate);
        //处理数据
        cluePoolPrivate.setCluePoolPrivateId(UUID.randomUUID().toString().replaceAll("-", ""));
        cluePoolPrivate.setCreatePeopleId(userAccount.getAccountId());
        cluePoolPrivate.setCreateTm(new Date());
        cluePoolPrivate.setUpdatePeopleId(userAccount.getAccountId());
        cluePoolPrivate.setUpdateTm(new Date());
        //所属公司ID
        cluePoolPrivate.setCompanyId(userAccount.getCompanyId());
        //默认私有池容量300条
        cluePoolPrivate.setCapacity(300);
        //判断是否是超级管理员，不是超级管理员就设置公司名
        if(!CommonUtils.isAdmin(userAccount.getUserId())){
            //所属公司名称
            SysCompany sysCompany = sysCompanyService.selectSysCompanyById(userAccount.getCompanyId());
            cluePoolPrivate.setCompanyName(sysCompany.getCompanyName());
        }
        Boolean result = cluePoolPrivateDbService.insertCluePoolPrivateInfo(cluePoolPrivate);
        return result;
    }


}
